package com.bmadmin.controller.sms;

import javax.servlet.http.HttpSession;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.aisino.sms.client.SmsClient;
import com.bmadmin.domain.MessageDomain;
import com.bmadmin.domain.NormalResult;
import com.bmadmin.domain.SysUsersPOJO;
import com.bmadmin.service.DelaySendService;
import com.bmadmin.service.SingleSendService;
import com.bmadmin.util.BMUtil;
import com.bmadmin.util.Constants;
import com.bmadmin.util.Util;

@RestController
@RequestMapping("/single")
public class SingleSendController {
	private Logger logger = LogManager.getLogger(this.getClass().getName());
	@Autowired
	SingleSendService singleSendService;
	@Autowired
	DelaySendService delaySendService;
    /**
     * 短信发送
     */
    @PostMapping("/sendmessage")
    public NormalResult sendmessage(HttpSession session,MessageDomain message) {
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
        NormalResult result = new NormalResult();
        try {
        	String delay_flag = message.getDelay_flag();
        	if(delay_flag.equals("N")){//立即发送
        		SmsClient smsClient = new SmsClient();
        		String phones =message.getS_phone();//要发送短信的号码数组
        		String nr = message.getS_message();//要发送的消息
        		String id = "1330212";//接入用户ID
        		String password = "LFGA6B02";//接入用户密码
        		String sn = Util.getSn();//序列号
        		String sender = "宁波市杭州湾新区国家税务局";//发送人，预留
        		String priority = "2";//优先级
        		String retResult = smsClient.sendMessageMToO(id, password, sn, phones, nr, sender,priority);
        		if(retResult.equals("0")){
        			logger.info("调用市局短信发送接口返回成功，手机号码【"+phones+"】");
            		message.setS_create_id(user.getU_id());
                	message.setS_state("Y");
                	message.setS_type("0");
                	message.setS_create_time(BMUtil.getNowTimeForString());
                	message.setS_sendtime(BMUtil.getNowTimeForString());
                	singleSendService.InsertMessageLog(message);
                	result.setCode("0000");
                    result.setMsg("短信发送成功！");
        		}else{
        			logger.info("调用市局短信发送接口返回失败，手机号码【"+phones+"】，短信内容【"+nr+"】，返回信息【"+retResult+"】");
        			result.setCode("9999");
                    result.setMsg("市局接口返回发送失败！");
        		}
        	}else{//定时发送
        		logger.info("保存至定时发票列表，手机号码【"+message.getS_phone()+"】");
        		message.setS_create_id(user.getU_id());
        		message.setS_create_time(BMUtil.getNowTimeForString());
            	message.setS_sendtime(BMUtil.getNowTimeForString());
            	delaySendService.InsertMessageDelay(message);
            	result.setCode("0000");
                result.setMsg("定时发送成功！");
        	}
        } catch (Exception e) {
            result.setCode("9999");
            result.setMsg("短信发送异常！");
            logger.error(e);
        }
        return result;
    }
}
